package com.gientech.springbootdrools.controller;


import com.gientech.springbootdrools.entity.Order;
import com.gientech.springbootdrools.service.RuleService;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private RuleService ruleService;

    @Autowired
    private KieBase kieBase;

    @GetMapping("/matchOrder")
    public Order saveOrder(@RequestBody Order order){
        Order result = ruleService.executeRule(order);
        //使用postman测试，需要在head里，将Content-Type 设置为 application/json
        return result;
    }

    @GetMapping("/test")
    public String test(){
        KieSession kieSession = kieBase.newKieSession();

        Order order = new Order();
        order.setOriginalPrice(160d);

        // 将order对象插入工作内存
        kieSession.insert(order);

        System.out.println("匹配规则前优惠后价格：" + order.getRealPrice());

        // 匹配对象
        // 激活规则，由drools框架自动进行规则匹配。若匹配成功，则执行
        kieSession.fireAllRules();

        // 关闭会话
        kieSession.dispose();

        System.out.println("优惠前价格：" + order.getOriginalPrice() + "\n优惠后价格：" + order.getRealPrice());

        return "success";
    }
}
